from .. import models

class select():

    def __init__(self):
        self.type = []
        self.price = ['全部','0-20','21-30','31-40','41-50','51-60','61-70','71-80','80以上']
        self.difficult = ['全部','入门','中级','高级']
        self.author = []
        self.select_item = {}
        self.selected = {'type':['','全部'],'author':['','全部'],'price':['全部','',''],'difficult':['','全部'],'keyword':'','order':0}
        self.books_list = []

    def get_type(self):
        self.type = ['全部']
        type_list = models.Booktype.objects.all().values('typename')
        for type in type_list:
            self.type.append(type['typename'])

    def get_author(self):
        self.author = ['全部']
        author_list = models.Author.objects.all().values('authorname')
        for author in author_list:
            self.author.append(author['authorname'])

    def get_all(self):
        self.get_type()
        self.get_author()
        self.select_item = {'type':self.type,'author':self.author,'price':self.price,'difficult':self.difficult}

    def clear_selected(self):
        self.selected = {'type':['','全部'],'author':['','全部'],'price':['全部','',''],'difficult':['','全部'],'keyword':'','order':0}

    def get_selected(self,name,type):
        print('得到的type是{}'.format(type))
        print('得到的name是{}'.format(name))
        if type == '类别：':
            if name == '全部':
                self.selected['type'][0] = ''
                self.selected['type'][1] = name
                print('走到了A')
            elif name == ' 全部':
                self.selected['type'][0] = ''
                self.selected['type'][1] = name
                print('走到了A')
            else:
                print('走到了B')
                type_obj = models.Booktype.objects.filter(typename=name).values()
                self.selected['type'][0] = type_obj[0]['id']
                self.selected['type'][1] = name
        elif type == '价格：':
            if name == '全部':
                self.selected['price'][0] = name
                self.selected['price'][1] = ''
            elif '0-20' in name:
                self.selected['price'][0] = name
                self.selected['price'][1] = 0
                self.selected['price'][2] = 20
            elif '21-30' in name:
                self.selected['price'][0] = name
                self.selected['price'][1] = 21
                self.selected['price'][2] = 30
            elif '31-40' in name:
                self.selected['price'][0] = name
                self.selected['price'][1] = 31
                self.selected['price'][2] = 40
            elif '41-50' in name:
                self.selected['price'][0] = name
                self.selected['price'][1] = 41
                self.selected['price'][2] = 50
            elif '51-60' in name:
                self.selected['price'][0] = name
                self.selected['price'][1] = 51
                self.selected['price'][2] = 60
            elif '61-70' in name:
                self.selected['price'][0] = name
                self.selected['price'][1] = 61
                self.selected['price'][2] = 70
            elif '71-80' in name:
                self.selected['price'][0] = name
                self.selected['price'][1] = 71
                self.selected['price'][2] = 80
            else:
                self.selected['price'][0] = name
                self.selected['price'][1] = 81
                self.selected['price'][2] = 99999
        elif type == '难度：':
            self.selected['difficult'][1] = name
        else:
            if name == '全部':
                self.selected['author'][0] = ''
                self.selected['author'][1] = name
            elif name == ' 全部':
                self.selected['author'][0] = ''
                self.selected['author'][1] = name
            else:
                type_obj = models.Author.objects.filter(authorname=name).values()
                self.selected['author'][0] = type_obj[0]['authorid']
                self.selected['author'][1] = name

    def get_books(self):
        # 展示全部书
        self.books_list = models.Book.objects.all().values()
        # 筛选类别
        if self.selected['type'][0] != '':
            self.books_list = self.books_list.filter(booktype=self.selected['type'][0]).values()
        # 筛选作者
        if self.selected['author'][0] != '':
            self.books_list = self.books_list.filter(bookauthor=self.selected['author'][0]).values()
        # 筛选价格
        if self.selected['price'][1] != '':
            self.books_list = self.books_list.filter(bookprice__range=(self.selected['price'][1], self.selected['price'][2])).values()
        # 筛选关键字
        if self.selected['keyword'] != '':
            self.books_list = self.books_list.filter(bookname__icontains=self.selected['keyword']).values()
        # 排序方式
        if self.selected['order'] == 1:
            self.books_list = self.books_list.order_by('bookprice')
        if self.selected['order'] == 2:
            self.books_list = self.books_list.order_by('-bookprice')


